

import game_thread
import pickle


class QueueWrapper(object):
    def __init__(self, conn):
        self.conn = conn

    def put(self, args):

#        print 'QueueWrapper.put: %s' % repr(args)
        (q, trans_id, cmd, cmd_args) = args

        if cmd == 'register_client':
            self.conn.sendall(pickle.dumps((trans_id, cmd, (cmd_args[0],))))
        else:
            self.conn.sendall(pickle.dumps((trans_id, cmd, cmd_args)))


class ProxySenderHandler(game_thread.Handler):
    def __init__(self, incoming_q, name, outgoing_conn):
        super(ProxySenderHandler, self).__init__(incoming_q)
        self.conn = outgoing_conn
        self.name = name

    def __getattr__(self, fn_name):

#        print '%s: Received send message: %s' % (self.name, fn_name)
        

        def __anon__(*args):

#            print '%s: Sending function name: %s%s' % (self.name, fn_name, repr(args))

            if fn_name == 'register_client':
                self.conn.sendall(pickle.dumps( (fn_name, args[ 0 ])))
            else:
                self.conn.sendall(pickle.dumps( (fn_name,) + args))

        return __anon__
        

class ProxySender(game_thread.GameThread):
    def __init__(self, name, outgoing_conn):
        super(ProxySender, self).__init__('ProxySender %s' % repr(name), ProxySenderHandler, name, outgoing_conn)
